import { defineStore } from 'pinia'
import wsClient, { reconnectWebSocket } from '@/utils/websocket'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: null,
  }),

  getters: {
    isLoggedIn: (state) => !!state.token,
  },

  actions: {
    async login(loginData) {
      try {
        // 登录逻辑
        // ...
        
        // 登录成功后重新检查连接
        reconnectWebSocket();
      } catch (error) {
        // 错误处理
        console.error('登录失败:', error);
      }
    },

    async logout() {
      // 登出逻辑
      this.token = '';
      this.userInfo = null;
      localStorage.removeItem('token');
      
      // 关闭WebSocket连接
      wsClient.close();
    }
  }
}) 